package bam.action;

import java.util.Map;

import org.json.JSONArray;


import com.opensymphony.xwork2.ActionContext;

import net.sf.json.JSONObject;
import bam.actionoper.UserCase;
import bam.entity.Logins;
import bam.entity.Mapspoor;
import bam.entity.Useraccount;
import bam.entity.Userinfo;
import bam.service.AreaService;
import bam.service.DealInfoService;
import bam.service.UserService;
import bam.service.UserinfoService;
import bam.util.DateUtil;
import bam.util.PagingUtil;

public class UserCaseAction {
	private AreaService areaService;
	private UserinfoService userinfoService;
	private UserService userService;
	private DealInfoService dealInfoService;
	
	String result;
	
	private String sousuo="";
	private String netType="";
	private String areaid="-1";
	
	private int pageNum;
	private int pageSize;
	private int logCount;
	private int pageCount;
	
	private String userid="";
	/**
	 * 
	 * �������
	 * @return
	 */
	public String caseTable(){
		try {
			int selectAreaNum = areaService.selectAreaNum();
			int selectOnlineNum = userinfoService.selectOnlineNum();
			int selectUserNum = userinfoService.selectUserNum();
			int selectNetNum = userinfoService.selectNetNum();
			System.out.println(selectAreaNum +"  "+selectOnlineNum+"  "+selectUserNum+"  "+selectNetNum);
			JSONObject json = new JSONObject();
			json.put("areaNum", selectAreaNum-1);
			json.put("onlineNum", selectOnlineNum);
			json.put("userNum", selectUserNum);
			json.put("netNum", selectNetNum);
			result= json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(result);
		return "caseTable";
	}
	
	/**
	 * �û�
	 * @return
	 */
	public String usersList(){
		PagingUtil<Userinfo> pu=new PagingUtil<Userinfo>();
		if(pageNum!=0){
			pu.setPageNo(pageNum);
			pu.setPageSize(pageSize);
			pu.setLogCount(logCount);
			pu.setPageCount(pageCount);
		}
		System.out.println("sousuo----------:"+sousuo);
		try {
			userinfoService.slurPageList(pu,sousuo );
			Map<String, Object> request = ActionContext.getContext().getContextMap();	
			request.put("page", pu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "usersList";
	}
	
	/**
	 * �û�
	 * @return
	 */
	public String usersListA(){
		PagingUtil<Useraccount> pu=new PagingUtil<Useraccount>();
		if(pageNum!=0){ 
			pu.setPageNo(pageNum);
			pu.setPageSize(pageSize);
			pu.setLogCount(logCount);
			pu.setPageCount(pageCount);
		}
		JSONObject sendJson = new JSONObject();
		System.out.println("sousuo----------:"+sousuo);
		try {
			userService.selUserList(pu, sousuo,  DateUtil.getparse("2010-01-01 00:00:00"));
			JSONArray list  = new JSONArray();
			if(!pu.getList().isEmpty()){
				for (int i = 0; i < pu.getList().size(); i++) {
					Useraccount ua = pu.getList().get(i);
					System.out.println(ua.getAccount());
					JSONObject j = new  JSONObject();
					j.put("userid", ua.getUsers().getId());
					j.put("userAccount", ua.getUsers().getNiuNum());
					j.put("userName", ua.getUsers().getNickname());
					j.put("userPhone", ua.getAccount());
					list.put(j);
				}
			}
			
			sendJson.put("pageNum", pu.getPageNo());
			sendJson.put("pageCount", pu.getPageCount());
			sendJson.put("logCount", pu.getLogCount());
			sendJson.put("pageSize", pu.getPageSize());
			sendJson.put("users", list.toString());
			sendJson.put("code", 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		result=sendJson.toString();
		return "usersListA";
	}
	
	
	/**
	 * �û���¼��¼
	 * ��ҳ
	 * @return
	 */
	public String usersLoginRecord(){
		PagingUtil<Logins> pu=new PagingUtil<Logins>();
		if(pageNum!=0){
			pu.setPageNo(pageNum);
			pu.setPageSize(pageSize);
		}
		try {
			userinfoService.selUserLoginList(pu, sousuo, Integer.parseInt(areaid));
			
			Map<String, Object> request = ActionContext.getContext().getContextMap();	
			
			System.out.println("********"+pu.getList().get(0).getLoginTime());
			request.put("page", pu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return "usersLoginRecord";
	}
	
	/**
	 * �û������¼
	 * ��ҳ
	 * @return
	 */
	public String usersNetRecord(){
		PagingUtil<Mapspoor> pu=new PagingUtil<Mapspoor>();
		if(pageNum!=0){
			pu.setPageNo(pageNum);
			pu.setPageSize(pageSize);
		}
		JSONObject sendjson  = new JSONObject();
		JSONArray jsonlist = new JSONArray();
		try {
			userinfoService.selUserNetList(pu,sousuo, areaid,netType,userid);
			if(pu.getList()!=null&&!pu.getList().isEmpty()){
				for (int i = 0; i < pu.getList().size(); i++) {
					JSONObject json = new JSONObject();
					Mapspoor mapspoor = pu.getList().get(i);
					json.put("netTimesCount", mapspoor.getPlanTime());
					json.put("surplusTimes", mapspoor.getSurplusMin());
					json.put("endTime", DateUtil.getformat(mapspoor.getOutnetTime()));
					json.put("startTime", DateUtil.getformat(mapspoor.getGetnetTime()));
					json.put("transactionID", mapspoor.getNetOrder());
					json.put("userid",mapspoor.getUsers().getId() );
					jsonlist.put(json);
				}
			}
			sendjson.put("list", jsonlist.toString());
			sendjson.put("code", 1);
			sendjson.put("pageNum", pu.getPageNo());
			sendjson.put("pageSize", pu.getPageSize());
			sendjson.put("pageCount", pu.getPageCount());
			sendjson.put("logCount", pu.getLogCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
		result=sendjson.toString();
		
		return "usersNetRecord";
	}
	
	/**
	 * 
	 * �û�����
	 * @return
	 */
	public String userDetail(){
		JSONObject sendjson  = new JSONObject();
		UserCase userCase = new UserCase(sendjson);
		try {
			userCase.userDetail(Integer.parseInt(userid), userService, userinfoService, dealInfoService);
			
		} catch (Exception e) {
			userCase.sendjson.put("code", -1);
			e.printStackTrace();
		}
		System.out.println( userCase.sendjson.toString());
		result= userCase.sendjson.toString();
		return "userDetail";
	}
	
	
	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setLogCount(int logCount) {
		this.logCount = logCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getResult() {
		return result;
	}

	public void setSousuo(String sousuo) {
		this.sousuo = sousuo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setDealInfoService(DealInfoService dealInfoService) {
		this.dealInfoService = dealInfoService;
	}

	public void setNetType(String netType) {
		this.netType = netType;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}
	
}
